//
//  UIDevice+TS.swift
//  TSKit
//
//  Created by chenyusen on 2018/3/16.
//

import Foundation

public extension UIDevice {
    
    public static var isIPhoneX: Bool {
        let identifier = mapToDevice(identifer: UIDevice.identifier)
        switch identifier {
        case "iPhone10,3", "iPhone10,6":
            return true
        default:
            return false
        }
    }
    
    private static var identifier: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let mirror = Mirror(reflecting: systemInfo.machine)
        
        let identifier = mirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }
    

    private static func mapToDevice(identifer: String) -> String {
        switch identifer {
        case "i386", "x86_64":
            return mapToDevice(identifer: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS")
        default:
            return identifer
        }
    }
    
    
}
